// Author: Domien Nowicki, Bjorn Schobben

#include <assert.h>
#include <QMenu>
#include <QColorDialog>
#include "TextureManagerView.h"


TextureManagerView::TextureManagerView(QWidget *parent): QListView(parent), textureModel(0)
{
	// Setup the treeview popup actions
	colorAction = new QAction("Select new user color", this);
	colorAction->setStatusTip("Give the selected texture a new internal color");
	colorAction->setIcon(QPixmap("icons/crayon.png"));
	selectAction = new QAction("Select texture", this);
	selectAction->setStatusTip("Select texture for painting walls with it");
	selectAction->setIcon(QPixmap("icons/selecttex.png"));
	removeAction = new QAction("Remove texture", this);
	removeAction->setStatusTip("Remove texture from the list");
	removeAction->setIcon(QPixmap("icons/removefile.png"));
	addSolidAction = new QAction("Add solid color texture", this);
	addSolidAction->setStatusTip("Add solod color texture to the list");
	addSolidAction->setIcon(QPixmap("icons/addfile.png"));

	QObject::connect(colorAction, SIGNAL(triggered()), this, SLOT(slotColor()));
	QObject::connect(selectAction, SIGNAL(triggered()), this, SLOT(slotSelect()));
	QObject::connect(removeAction, SIGNAL(triggered()), this, SLOT(slotRemove()));
	QObject::connect(addSolidAction, SIGNAL(triggered()), this, SLOT(slotAddSolid()));

	// Specify our custom item delegate
	setItemDelegate(new TextureItemDelegate(this));

	// Specify our thumb size
	setIconSize(QSize(THUMBSIZE, THUMBSIZE));
}

TextureManagerView::~TextureManagerView()
{
}

// Slot used to change the user color in the texture manager
void TextureManagerView::slotColor()
{
	unsigned int tindex = GetCurrentTextureIndex();

	// Can't change user color for empty texture
	if (tindex == -1)
		return;

	// Ask which color to change to
	QColor color = QColorDialog::getColor(Qt::white, this);

	if (color.isValid())
	{
		Pixel pixel(color.red(), color.green(), color.blue());

		emit changeUserColor(tindex, pixel);
	}
}



// Slot for selecting a texture
void TextureManagerView::slotSelect()
{
	// Tell listeners that we have selected a texture
	emit selectTexture();
}

// Slot for removing a texture
void TextureManagerView::slotRemove()
{
	unsigned int tindex = GetCurrentTextureIndex();

	// Can't change user color for empty texture
	if (tindex == -1)
		return;

	// Tell listeners to remove texture
	emit removeTexture();
}

// Slot for adding a solid color texture
void TextureManagerView::slotAddSolid()
{
	// Tell listeners to add solid texture
	emit addSolidTexture();
}


// Set the texture manager model
void TextureManagerView::SetModel(TextureManagerModel *texModel)
{
	textureModel = texModel;

	setModel(textureModel);
}

// Return the texture manager model
TextureManagerModel* TextureManagerView::GetModel() const
{
	return textureModel;
}

// Add a texture to the texture model
unsigned int TextureManagerView::AddTexture(const Texture& texture)
{
	assert(0 != textureModel);

	return textureModel->AddTexture(texture);
}

// Return the amount of textures currently in the texture model
unsigned int TextureManagerView::GetTextureCount() const
{
	assert(0 != textureModel);

	return textureModel->GetTextureCount();
}

// Return a texture
const Texture& TextureManagerView::GetTexture(unsigned int tindex) const
{
	assert(0 != textureModel);

	return textureModel->GetTexture(tindex);
}

// Remove a texture
void TextureManagerView::RemoveTexture(unsigned int index)
{
	assert(0 != textureModel);

	textureModel->RemoveTexture(index);
}

// Return the texture thumb
const QPixmap& TextureManagerView::GetTextureThumb(unsigned int tindex) const
{
	assert(0 != textureModel);

	return textureModel->GetTextureThumb(tindex);
}

// Return the current texture index
unsigned int TextureManagerView::GetCurrentTextureIndex() const
{
	return currentIndex().row();
}


QSize TextureManagerView::sizeHint() const
{
	return QSize(100,200);
}

QSize TextureManagerView::minimumSizeHint() const
{
	return QSize(100, 200);
}

QSize TextureManagerView::maximumSizeHint() const
{
	return QSize(400, 200);
}

// Called when user right-clicks
void TextureManagerView::contextMenuEvent(QContextMenuEvent *e)
{
	unsigned int tindex = GetCurrentTextureIndex();

	QMenu menu(this);

	// Update enabled/disabled state

	selectAction->setEnabled(true);
	addSolidAction->setEnabled(true);
	if (-1 == tindex)
	{
		colorAction->setEnabled(false);
		removeAction->setEnabled(false);
	}
	else
	{
		colorAction->setEnabled(true);
		removeAction->setEnabled(true);
	}
	
	// Add actions
	menu.addAction(selectAction);
	menu.addAction(colorAction);	
	menu.addAction(removeAction);
	menu.addAction(addSolidAction);

	// Show menu from current position
	menu.exec(e->globalPos());
}